package cn.buk.qms.task;

import cn.buk.qms.service.*;
import cn.buk.tms.bill.service.BillCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定时任务
 *
 * @author yfdai
 */
@Profile({"prod"})
@Component
public class MainTask {

  private static final int SERVER_MAIN = 1;

  /**
   * 当前应用所在服务器的id
   */
  private final int serverId;

  /**
   * 当前应用所属的拥有者在数据库中对应的企业id
   */
  private final int ownerId;

  private String getAgentName() {
    return "system-" + serverId;
  }

  private final BillService billService;
  private final BillAutoTaskService billAutoTaskService;
  private final ReportService reportService;
  private final EnterpriseService enterpriseService;
  private final EnterpriseStatService enterpriseStatService;
  private final BillCheckService billCheckService;

  @Autowired
  public MainTask(@Value("${server_id}") int serverId,
                  @Value("${owner_enterprise_id}") int ownerId,
                  BillService billService,
                  BillAutoTaskService billAutoTaskService,
                  ReportService reportService,
                  EnterpriseService enterpriseService,
                  EnterpriseStatService enterpriseStatService,
                  BillCheckService billCheckService) {
    this.serverId = serverId;
    this.ownerId = ownerId;

    this.billService = billService;
    this.billCheckService = billCheckService;
    this.billAutoTaskService = billAutoTaskService;
    this.reportService = reportService;
    this.enterpriseService = enterpriseService;
    this.enterpriseStatService = enterpriseStatService;
  }

  /**
   * .
   * 将账单金额记录到企业应付款中
   */
  @Async
  @Scheduled(cron = "0 30 0 * * *")
  public void syncBillInfo2AmountBalance() {
    if (serverId != SERVER_MAIN) {
      return;
    }

    // 将BillInfo(t301)中新来的账单的金额统计到客户应付款中
    billCheckService.syncBillInfo2Balance();

  }

  /**
   * 将账单的销账状态反馈到订单中
   */
  @Async
  @Scheduled(cron = "0 0/30 8-22 * * *")
  public void syncBillPaidStatus() {
    if (serverId != SERVER_MAIN) {
      return;
    }

    final int max = 20000;

    billAutoTaskService.autoSyncBillPaid2FlightOrder(getAgentName(), max);
    billAutoTaskService.autoSyncBillPaid2RefundOrder(getAgentName(), max);
    billAutoTaskService.autoSyncBillPaid2ChangeOrder(getAgentName(), max);

    billAutoTaskService.autoSyncBillPaid2VasOrder(getAgentName(), max);

    billAutoTaskService.autoSyncBillPaid2HotelOrder(getAgentName(), max);
    billAutoTaskService.autoSyncBillPaid2TrainOrder(getAgentName(), max);
  }


  /**
   * 检查服务账单中的行程详情
   */
  @Async
  @Scheduled(cron = "0 0/30 * * * *")
  public void autoCheckBillOrderDate() {
    if (serverId != SERVER_MAIN) {
      return;
    }

    billService.autoUpdateBillVas(getAgentName());
  }


  /**
   * 礼拜一早上8点30分统计一次，六个月内未支付金额最多的头10名，按每月
   */
  @Async
  @Scheduled(cron = "0 30 8 * * 1")
  public void autoAlertUnpaidMonthlyStats() {
    if (serverId != SERVER_MAIN) {
      return;
    }

    reportService.autoAlertMonthlyUnpaidTop10();
  }


  /**
   * 每天晚上9点3统计一次，3个月内没有账单的客户
   * <p>
   * cron一般是6个部分或是7个部分：如下所示：
   * 1. Seconds （秒）
   * 2. Minutes （分）
   * 3. Hours （时）
   * 4. Day-of-Month （天）
   * 5. Month （月） Month(月) ：可以用0-11 或用字符串 “JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC” 表示；
   * 6. Day-of-Week （周） Day-of-Week(*每周*)*：*可以用数字1-7表示（1 ＝ 星期日）或用字符口串“SUN, MON, TUE, WED, THU, FRI and SAT”表示；
   * 7. Year (年 可选字段)
   */
  @Async
  @Scheduled(cron = "0 0 21 * * 1")
  public void autoAlertNoBusinessCustomer() {
    if (serverId != SERVER_MAIN) {
      return;
    }

    if (this.ownerId < 1) {
      return;
    }

    reportService.autoAlertNoBusinessCustomer(this.ownerId, 100);
  }


  /**
   * 每天早上0点30分，把机票订单中的乘机人信息导入到Passenger(t601)中
   */
  @Async
  @Scheduled(cron = "0 30 0 * * *")
  public void importPassenger() {
    if (serverId != SERVER_MAIN) {
      return;
    }

    try {
      enterpriseService.importPassengers(this.ownerId);
    } catch (Exception ex) {
      ex.printStackTrace();
    }

  }


  /**
   * 清理重复的
   */
  @Async
  @Scheduled(cron = "0 30 6 * * *")
  public void cleanDuplicatePassenger() {
    if (serverId != SERVER_MAIN) {
      return;
    }
    enterpriseService.cleanDuplicatePassenger(this.ownerId);
  }


  @Async
  @Scheduled(cron = "0 0 5 * * *")
  public void refreshCustomerRecency() {
    if (serverId != SERVER_MAIN) {
      return;
    }
    enterpriseService.refreshCustomerRecency();
  }

  @Async
  @Scheduled(cron = "0 5 5 * * *")
  public void refreshPassengerRecency() {
    if (serverId != SERVER_MAIN) {
      return;
    }
    enterpriseStatService.refreshPassengerRecency();
  }
}
